package com.ebelter.myehc.ui.fragments.trends;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.bluetooth.products.scale.ScaleManager;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.CommonUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.views.ChartView3;
import com.ebelter.btcomlib.views.CombinView7;
import com.ebelter.myehc.R;
import com.ebelter.myehc.adapters.TrendScaleFragmentAdaper;
import com.ebelter.myehc.beans.TrendScaleAdaperBean;
import com.ebelter.myehc.models.http.response.device_resports.BC_R;
import com.ebelter.myehc.utils.EHCCommonUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;


public class Trend_Scale_Fragment extends BaseFragment {
    public static final String TAG = "Trend_Scale_Fragment";

    private static final int[] levelColors = {0xff18d5e5, 0xff14e34e, 0xffffcc00, 0xfffc3838};

    CombinView7 scale_cb7;
    RecyclerView recyclerView;
    TrendScaleFragmentAdaper adaper;

    ChartView3 scale_weight_chartview3;
    ChartView3 scale_fat_chartview3;
    ChartView3 scale_muscle_chartview3;
    ChartView3 scale_bmr_chartview3;
    ChartView3 scale_infat_chartview3;
    ChartView3 scale_water_chartview3;
    ChartView3 scale_bond_chartview3;
    ChartView3 scale_protein_chartview3;
    ChartView3 scale_bmi_chartview3;

    private void FV() {
        recyclerView = mRootView.findViewById(R.id.scale_rv);
        scale_cb7 = mRootView.findViewById(R.id.scale_cb7);
        if (AppUtils.en==AppUtils.getLocalStr()){
            scale_cb7.setTitleSizes(13);
        }
        scale_weight_chartview3 = mRootView.findViewById(R.id.scale_weight_chartview3);
        scale_weight_chartview3.warringTem = 1000;
        scale_weight_chartview3.yOriStart = 0;

        scale_fat_chartview3 = mRootView.findViewById(R.id.scale_fat_chartview3);
        scale_fat_chartview3.warringTem = 1000;
        scale_fat_chartview3.yOriStart = 0;

        scale_muscle_chartview3 = mRootView.findViewById(R.id.scale_muscle_chartview3);
        scale_muscle_chartview3.warringTem = 1000;
        scale_muscle_chartview3.yOriStart = 0;

        scale_bmr_chartview3 = mRootView.findViewById(R.id.scale_bmr_chartview3);
        scale_bmr_chartview3.warringTem = 6000;
        scale_bmr_chartview3.yOriStart = 0;
        scale_bmr_chartview3.displayInt=true;

        scale_infat_chartview3 = mRootView.findViewById(R.id.scale_infat_chartview3);
        scale_infat_chartview3.warringTem = 1000;
        scale_infat_chartview3.yOriStart = 0;

        scale_water_chartview3 = mRootView.findViewById(R.id.scale_water_chartview3);
        scale_water_chartview3.warringTem = 1000;
        scale_water_chartview3.yOriStart = 0;

        scale_weight_chartview3 = mRootView.findViewById(R.id.scale_weight_chartview3);
        scale_weight_chartview3.warringTem = 1000;
        scale_weight_chartview3.yOriStart = 0;

        scale_bond_chartview3 = mRootView.findViewById(R.id.scale_bond_chartview3);
        scale_bond_chartview3.warringTem = 1000;
        scale_bond_chartview3.yOriStart = 0;

        scale_protein_chartview3 = mRootView.findViewById(R.id.scale_protein_chartview3);
        scale_protein_chartview3.warringTem = 1000;
        scale_protein_chartview3.yOriStart = 0;

        scale_bmi_chartview3 = mRootView.findViewById(R.id.scale_bmi_chartview3);
        scale_bmi_chartview3.warringTem = 1000;
        scale_bmi_chartview3.yOriStart = 0;
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        adaper = new TrendScaleFragmentAdaper(getContext());

        List<TrendScaleAdaperBean> datas = new ArrayList<>();

        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Weight), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Body_fat_percentage), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Muscles_mass), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Basal_metabolic_rate), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Visceral_fat_level), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Body_water_percentage), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Bone_mineral_content), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Protein), N2, N2, N2,0));
        datas.add(new TrendScaleAdaperBean(getString_(R.string.str_BMI), N2, N2, N2,0));

        recyclerView.setAdapter(adaper);
        adaper.setDatas(datas);

    }

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_trend_scale;
    }


    public void setReportData(BC_R reportData) {
        if (reportData == null || reportData.resultCode != 1 || reportData.resultData == null) {
            return;
        }




        //x轴坐标对应的数据
        List<String> weightXValue = new ArrayList<>();
        List<String> fatXValue = new ArrayList<>();
        List<String> muscleXValue = new ArrayList<>();
        List<String> bmrXValue = new ArrayList<>();
        List<String> infatXValue = new ArrayList<>();
        List<String> waterXValue = new ArrayList<>();
        List<String> boneXValue = new ArrayList<>();
        List<String> proteinXValue = new ArrayList<>();
        List<String> bmiXValue = new ArrayList<>();

        List<String> weightDisXValue = new ArrayList<>();
        List<String> fatDisXValue = new ArrayList<>();
        List<String> muscleDisXValue = new ArrayList<>();
        List<String> bmrDisXValue = new ArrayList<>();
        List<String> infatDisXValue = new ArrayList<>();
        List<String> waterDisXValue = new ArrayList<>();
        List<String> boneDisXValue = new ArrayList<>();
        List<String> proteinDisXValue = new ArrayList<>();
        List<String> bmiDisXValue = new ArrayList<>();

        List<Integer> bmiPointColor = new ArrayList<>();
        List<Integer> xPointColor = new ArrayList<>();

        Map<String, Float> weightValue = new HashMap<>();
        Map<String, Float> fatValue = new HashMap<>();
        Map<String, Float> muscleValue = new HashMap<>();
        Map<String, Float> bmrValue = new HashMap<>();
        Map<String, Float> infatValue = new HashMap<>();
        Map<String, Float> waterValue = new HashMap<>();
        Map<String, Float> boneValue = new HashMap<>();
        Map<String, Float> proteinValue = new HashMap<>();
        Map<String, Float> bmiValue = new HashMap<>();

        //dateType:日期类型 1:年 2:月 3周: 4:天
        //measureType:1:血糖 2:血压 3:血氧 4:体温 5:尿酸 6:总胆固醇 7:人体成份

        float newestWeight = 0;
        float newestAdiposerate = 0;
        float newestMuscle = 0;
        float newestBmr = 0;
        float newestInfat = 0;
        float newestMoisture = 0;
        float newestBone = 0;
        float newestProtein = 0;
        float newestBmi = 0;

        if (reportData.resultData.chartList != null) {

            for (int i = 0; i < reportData.resultData.chartList.size(); i++) {
                BC_R.ResultDataBean.ChartListBean data = reportData.resultData.chartList.get(i);
                if (TextUtils.isEmpty(data.timeParam)){
                    continue;
                }

                String timeParam = "";
                String disTimeParam = "";
                if (reportData.resultData.dateType == 1) {
                    Date date = TimeUtils.parseFormatter91Time(data.timeParam);
                    timeParam = CommonUtils.getMonths(NumUtils.string2Int(TimeUtils.formatTime12(date.getTime())));
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 2) {
                    timeParam = String.format(getString_(R.string.Week_int),data.timeParam);
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 3) {
                    Date date2 = TimeUtils.parseFormatter3Time(data.timeParam);
                    timeParam = TimeUtils.formatTime11(date2.getTime());
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 4) {
                    Date date3 = TimeUtils.parseFormatter1Time(data.timeParam);
                    timeParam = TimeUtils.formatTime13(date3.getTime());
                    disTimeParam = TimeUtils.formatTime4(date3.getTime());

                }


                if (data.weightAvg > 0) {
                    weightXValue.add(timeParam);
                    weightDisXValue.add(disTimeParam);
                    weightValue.put(timeParam, data.weightAvg);
                    newestWeight=data.weightAvg;
                }

                if (data.adiposerateAvg > 0) {//脂肪
                    fatXValue.add(timeParam);
                    fatDisXValue.add(disTimeParam);
                    fatValue.put(timeParam, data.adiposerateAvg);
                    newestAdiposerate=data.adiposerateAvg;
                }

                if (data.muscleAvg > 0) {
                    muscleXValue.add(timeParam);
                    muscleDisXValue.add(disTimeParam);
                    muscleValue.put(timeParam, data.muscleAvg);
                    newestMuscle = data.muscleAvg;
                }

                if (data.bmrAvg > 0) {
                    bmrXValue.add(timeParam);
                    bmrDisXValue.add(disTimeParam);
                    bmrValue.put(timeParam, data.bmrAvg);
                    newestBmr = data.bmrAvg;
                }

                if (data.visceralfatAvg > 0) {
                    infatXValue.add(timeParam);
                    infatDisXValue.add(disTimeParam);
                    infatValue.put(timeParam, data.visceralfatAvg);
                    newestInfat=data.visceralfatAvg;
                }

                if (data.moistureAvg > 0) {//水分
                    waterXValue.add(timeParam);
                    waterDisXValue.add(disTimeParam);
                    waterValue.put(timeParam, data.moistureAvg);
                    newestMoisture=data.moistureAvg;
                }

                if (data.boneAvg > 0) {
                    boneXValue.add(timeParam);
                    boneDisXValue.add(disTimeParam);
                    boneValue.put(timeParam, data.boneAvg);
                    newestBone=data.boneAvg;
                }

                if (data.proteinAvg > 0) {
                    proteinXValue.add(timeParam);
                    proteinDisXValue.add(disTimeParam);
                    proteinValue.put(timeParam, data.proteinAvg);
                    newestProtein=data.proteinAvg;
                }

                if (data.bmiAvg > 0) {
                    bmiXValue.add(timeParam);
                    bmiDisXValue.add(disTimeParam);
                    bmiValue.put(timeParam, data.bmiAvg);
                    newestBmi=data.bmiAvg;
                    bmiPointColor.add(levelColors[ScaleManager.getBMILevel(data.bmiAvg)]);
                }
                xPointColor.add(levelColors[1]);
            }

        }


        float maxInfatLevel = 8;

        if (reportData.resultData.summaryList != null && reportData.resultData.summaryList.size() > 0) {

            BC_R.ResultDataBean.SummaryListBean da = reportData.resultData.summaryList.get(0);

            List<TrendScaleAdaperBean> datas = new ArrayList<>();

            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Weight), NumUtils.disFlZero(NumUtils.g0Bl1(da.weightMin),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.weightMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.weightAvg),N2), EHCCommonUtils.getDisplaySrcRes(da.weightAvg,newestWeight)));
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Body_fat_percentage), NumUtils.disFlZero(NumUtils.g0Bl1(da.adiposerateMin) ,N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.adiposerateMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.adiposerateAvg),N2),EHCCommonUtils.getDisplaySrcRes(da.adiposerateAvg,newestAdiposerate)));
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Muscles_mass), NumUtils.disFlZero(NumUtils.g0Bl1(da.muscleMin) ,N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.muscleMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.muscleAvg),N2),EHCCommonUtils.getDisplaySrcRes(da.muscleAvg,newestMuscle)));
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Basal_metabolic_rate), NumUtils.disIntZero(NumUtils.g0Bl0(da.bmrMin) ,N2), NumUtils.disIntZero(NumUtils.g0Bl0(da.bmrMax),N2), NumUtils.disIntZero(NumUtils.g0Bl0(da.bmrAvg),N2),EHCCommonUtils.getDisplaySrcRes(da.bmrAvg,newestBmr)));

            maxInfatLevel = NumUtils.g0Bl1(da.visceralfatMax);
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Visceral_fat_level), NumUtils.disFlZero(NumUtils.g0Bl1(da.visceralfatMin) ,N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.visceralfatMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.visceralfatAvg),N2),EHCCommonUtils.getDisplaySrcRes(da.visceralfatAvg,newestInfat)));
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Body_water_percentage), NumUtils.disFlZero(NumUtils.g0Bl1(da.moistureMin) ,N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.moistureMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.moistureAvg),N2),EHCCommonUtils.getDisplaySrcRes(da.moistureAvg,newestMoisture)));
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Bone_mineral_content), NumUtils.disFlZero(NumUtils.g0Bl1(da.boneMin) ,N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.boneMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.boneAvg),N2),EHCCommonUtils.getDisplaySrcRes(da.boneAvg,newestBone)));
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_Protein), NumUtils.disFlZero(NumUtils.g0Bl1(da.proteinMin) ,N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.proteinMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.proteinAvg),N2),EHCCommonUtils.getDisplaySrcRes(da.proteinAvg,newestProtein)));
            datas.add(new TrendScaleAdaperBean(getString_(R.string.str_BMI), NumUtils.disFlZero(NumUtils.g0Bl1(da.bmiMin) ,N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.bmiMax),N2), NumUtils.disFlZero(NumUtils.g0Bl1(da.bmiAvg) ,N2),EHCCommonUtils.getDisplaySrcRes(da.bmiAvg,newestBmi)));

            if (adaper != null) adaper.setDatas(datas);
        }


        //y轴坐标对应的数据
        List<Float> weightYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            weightYValue.add((float) (i * 30));
        }

        List<Float> fatYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            fatYValue.add((float) (i * 30));
        }

        List<Float> muscleYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            muscleYValue.add((float) (i * 30));
        }

        List<Float> bmrYValue = new ArrayList<>();
        for (int i = 0; i < 4; i++) {//Y轴的刻度值
            bmrYValue.add((float) (i * 1000));
        }

        List<Float> infatfatYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            infatfatYValue.add((float) (i * maxInfatLevel*1.0/5));
        }

        List<Float> waterYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            waterYValue.add((float) (i * 30));
        }

        List<Float> boneYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            boneYValue.add((float) (i * 1));
        }
        List<Float> proteinYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            proteinYValue.add((float) (i * 10));
        }

        List<Float> bmiYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            bmiYValue.add((float) (i * 10));
        }

        if (scale_weight_chartview3 != null) {
            scale_weight_chartview3.setxPointColor(xPointColor);
            scale_weight_chartview3.setdisXValue(weightDisXValue);
            scale_weight_chartview3.setValue(weightValue, weightXValue, weightYValue);
            scale_weight_chartview3.src2end();
        }

        if (scale_fat_chartview3 != null) {
            scale_fat_chartview3.setxPointColor(xPointColor);
            scale_fat_chartview3.setdisXValue(fatDisXValue);
            scale_fat_chartview3.setValue(fatValue, fatXValue, fatYValue);
            scale_fat_chartview3.src2end();
        }

        if (scale_muscle_chartview3 != null) {
            scale_muscle_chartview3.setxPointColor(xPointColor);
            scale_muscle_chartview3.setdisXValue(muscleDisXValue);
            scale_muscle_chartview3.setValue(muscleValue, muscleXValue, muscleYValue);
            scale_muscle_chartview3.src2end();
        }

        if (scale_bmr_chartview3 != null) {
            scale_bmr_chartview3.setxPointColor(xPointColor);
            scale_bmr_chartview3.setdisXValue(bmrDisXValue);
            scale_bmr_chartview3.setValue(bmrValue, bmrXValue, bmrYValue);
            scale_bmr_chartview3.src2end();
        }

        if (scale_infat_chartview3 != null) {//----------
            scale_infat_chartview3.setxPointColor(xPointColor);
            scale_infat_chartview3.setdisXValue(infatDisXValue);
            scale_infat_chartview3.setValue(infatValue, infatXValue, infatfatYValue);
            scale_infat_chartview3.src2end();
        }

        if (scale_water_chartview3 != null) {
            scale_water_chartview3.setxPointColor(xPointColor);
            scale_water_chartview3.setdisXValue(waterDisXValue);
            scale_water_chartview3.setValue(waterValue, waterXValue, waterYValue);
            scale_water_chartview3.src2end();
        }

        if (scale_bond_chartview3 != null) {
            scale_bond_chartview3.setxPointColor(xPointColor);
            scale_bond_chartview3.setdisXValue(boneDisXValue);
            scale_bond_chartview3.setValue(boneValue, boneXValue, boneYValue);
            scale_bond_chartview3.src2end();
        }

        if (scale_protein_chartview3 != null) {
            scale_protein_chartview3.setxPointColor(xPointColor);
            scale_protein_chartview3.setdisXValue(proteinDisXValue);
            scale_protein_chartview3.setValue(proteinValue, proteinXValue, proteinYValue);
            scale_protein_chartview3.src2end();
        }

        if (scale_bmi_chartview3 != null) {
            scale_bmi_chartview3.setxPointColor(bmiPointColor);
            scale_bmi_chartview3.setdisXValue(bmiDisXValue);
            scale_bmi_chartview3.setValue(bmiValue, bmiXValue, bmiYValue);
            scale_bmi_chartview3.src2end();
        }


    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }


    //最新的一条数据跟平均值来判断
    public int getDisplaySrcRes() {
        return 0;
    }
}
